package com.geekstudy.week3.http;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;

public class HttpClientProvider implements HttpInterface{


    @Override
    public String get(String url) {
        String responseText = null;
        System.out.println("HttpClientProvider url:" + url);
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            responseText = EntityUtils.toString(httpEntity);

        } catch(Exception e) {
            System.out.println("HttpClientProvider url:" + url + " exception:" + e.getMessage());
            e.printStackTrace();
        }

        return responseText;
    }
}
